// 判断各种类型

const _isType = function (type) {
  return function (obj) {
    return {}.toString.call(obj) == `[object ${type}]`;
  };
};

export const isStr = _isType('String');

export const isFunc = _isType('Function');

export const isUndef = _isType('undefined');

export const isNull = _isType('null');

export const isNullOrUndef = function (val) {
  return isNull(val);
};

export const isUri = function (path_) {
  const reg =
    /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
  return reg.test(path_);
};

export const clone = function (from_) {
  if (from_ == null || typeof from_ !== 'object') return from_;

  if (
    from_.constructor == Date ||
    from_.constructor == RegExp ||
    isFunc(from_) ||
    isStr(from_) ||
    from_.constructor == Number ||
    from_.constructor == Boolean
  )
    return new from_.constructor(from_);

  const to = new from_.constructor();
  for (const n in from_) {
    if (from_.hasOwnProperty(n)) {
      to[n] = typeof to[n] === 'undefined' ? clone(from_[n], null) : to[n];
    }
  }
  return to;
};
